//
// Description: 13. 罗马数字转整数
// Created by Loading on 2025/2/22.
//

#include <bits/stdc++.h>

using namespace std;

int romanToInt(string s) {
    int values[256];
    values['I'] = 1;
    values['V'] = 5;
    values['X'] = 10;
    values['L'] = 50;
    values['C'] = 100;
    values['D'] = 500;
    values['M'] = 1000;

    int res = 0;
    for (int i = 0; i < s.size(); ++i) {
        if (i + 1 < s.size() && values[s[i]] < values[s[i + 1]]) {
            res += values[s[i + 1]] - values[s[i]];
            ++i;
        } else {
            res += values[s[i]];
        }
    }

    return res;
}

int main() {
    string s = "MCMXCIV";

    auto res = romanToInt(s);

    cout << res << endl;

    return 0;
}